package park.gz.com.parkingfee.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import park.gz.com.parkingfee.R;
import park.gz.com.parkingfee.fragment.ParkfeeFragment;
import park.gz.com.parkingfee.fragment.QueryfeeFragment;
import park.gz.com.parkingfee.until.ServiceFile;


public class ParkActivity extends FragmentActivity implements View.OnClickListener, ViewPager.OnPageChangeListener {
    @Bind(R.id.tx_parkfee)
    TextView txParkfee;
    @Bind(R.id.tx_queryfee)
    TextView txQueryfee;
    @Bind(R.id.fl_manager)
    FrameLayout flManager;
    private ParkfeeFragment parkfeeFragment;
    private QueryfeeFragment queryfeeFragment;
    private RadioButton rbBack;
    private Context context;
    private long mBackKeyPressedTime;
    private ServiceFile serviceFile;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_fee);
        ButterKnife.bind(this);
        serviceFile = new ServiceFile();
        serviceFile.onCreate();
        initViews();
        setSelect(0);

    }

    public void initViews() {
        context = getApplicationContext();
        txParkfee.setBackgroundResource(R.color.RGB_ff8018);
        txParkfee.setTextColor(context.getResources().getColor(R.color.color2));
        txQueryfee.setBackgroundResource(R.color.color2);
        txQueryfee.setTextColor(context.getResources().getColor(R.color.RGB_ff8018));
    }



    private void setSelect(int i) {

        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();

        hideFragment(transaction);

        switch (i) {
            case 0:
                if (parkfeeFragment == null) {
                    parkfeeFragment = new ParkfeeFragment();
                    transaction.add(R.id.fl_manager, parkfeeFragment);
                } else {
                    transaction.show(parkfeeFragment);
                }
                txParkfee.setBackgroundResource(R.color.RGB_ff8018);
                txParkfee.setTextColor(context.getResources().getColor(R.color.color2));
                txQueryfee.setBackgroundResource(R.color.color2);
                txQueryfee.setTextColor(context.getResources().getColor(R.color.RGB_ff8018));
                break;
            case 1:
                if (queryfeeFragment == null) {
                    queryfeeFragment = new QueryfeeFragment();
                    transaction.add(R.id.fl_manager, queryfeeFragment);
                } else {
                    transaction.show(queryfeeFragment);

                }
                txQueryfee.setBackgroundResource(R.color.RGB_ff8018);
                txQueryfee.setTextColor(context.getResources().getColor(R.color.color2));
                txParkfee.setBackgroundResource(R.color.color2);
                txParkfee.setTextColor(context.getResources().getColor(R.color.RGB_ff8018));
                break;
        }

        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction) {
        if (parkfeeFragment != null) {
            transaction.hide(parkfeeFragment);
        }
        if (queryfeeFragment != null) {
            transaction.hide(queryfeeFragment);
        }

    }

    @OnClick({R.id.tx_parkfee, R.id.tx_queryfee})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tx_parkfee:
                setSelect(0);
                break;
            case R.id.tx_queryfee:
                setSelect(1);
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - mBackKeyPressedTime) > 1000) {
                Toast.makeText(ParkActivity.this, "再按一次退出程序", Toast.LENGTH_LONG).show();
                mBackKeyPressedTime = System.currentTimeMillis();

            } else {
                this.finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
